/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.nodes.objects.helpers;

import java.beans.IntrospectionException;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import org.openide.nodes.BeanNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import pl.edu.amu.wmi.kino.visualjavafx.model.api.ModelChangeEvent;
import pl.edu.amu.wmi.kino.visualjavafx.model.api.ModelChangeListener;
import pl.edu.amu.wmi.kino.visualjavafx.model.api.ModelChangeNotifier;

/**
 *
 * @author psychollek
 */
public class JavaFxNode<T> extends BeanNode<T> implements PropertyChangeListener,ModelChangeNotifier{

    private ArrayList<ModelChangeListener> listeners = new ArrayList<ModelChangeListener>();
    protected InstanceContent ic = new InstanceContent();
    private Lookup lookup = new AbstractLookup(ic);

    public JavaFxNode( T obj, Children chd ) throws IntrospectionException {
            super(obj, chd);
            ic.add(super.getLookup().lookupAll(Object.class));
            ic.add(this);
            ic.add(obj);
            this.addPropertyChangeListener(this);
    }

    public Object getModelObject(){
        return this.getBean();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getSource() == this){
            ModelChangeEvent mce = ModelChangeEvent.propertyChanged(this, getModelObject(), evt.getPropertyName());
            for (Iterator<ModelChangeListener> it = listeners.iterator(); it.hasNext();) {
                ModelChangeListener mcl = it.next();
                mcl.notifyModelChange(mce);
            }

        }
    }

    @Override
    public void addModelChangeListener(ModelChangeListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeModelChangeListener(ModelChangeListener listener) {
        listeners.remove(listener);
    }


}
